@file:Suppress("MemberVisibilityCanBePrivate")

package tech.summerly.weather.component.view

import android.content.Context
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.view_floor_header.view.*
import tech.summerly.weather.R
import tech.summerly.weather.util.color

/**
 * Created by summer on 18-3-15
 *
 * 主界面各天气楼层的标题栏
 *
 * 具体布局详见 [R.layout.view_floor_header]
 */
class FloorHeaderView(context: Context, attr: AttributeSet?, defStyleAttr: Int = 0) : FrameLayout(context, attr, defStyleAttr) {

    constructor(context: Context, attr: AttributeSet?) : this(context, attr, 0)


    init {
        LayoutInflater.from(context).inflate(R.layout.view_floor_header, this)
        attr?.let { context.obtainStyledAttributes(it, R.styleable.FloorHeaderView) }?.apply {
            val color = getColor(R.styleable.FloorHeaderView_color, color(R.color.color_text_primary, context))
            setColor(color)
            val title = getString(R.styleable.FloorHeaderView_title)
            setTitle(title)
            val showMoreAction = getBoolean(R.styleable.FloorHeaderView_showMoreButton, false)
            if (showMoreAction) {
                showMoreButton()
            } else {
                hideMoreButton()
            }
            recycle()
        }
    }


    /**
     * 设置所有元素的颜色
     */
    fun setColor(@ColorInt color: Int) {
        indicator.setBackgroundColor(color)
        title.setTextColor(color)
        textMore.setTextColor(color)
        iconMore.setColorFilter(color)
    }

    /**
     * 设置标题
     */
    fun setTitle(title: CharSequence) {
        this.title.text = title
    }


    /**
     * 显示右边的按钮
     *
     * @param action 按钮的文字，如果为空，则不改变原来的文字
     */
    fun showMoreButton(action: CharSequence? = null) {
        layoutMore.visibility = View.VISIBLE
        action?.let { textMore.text = it }
    }

    /**
     * 隐藏右边的按钮
     */
    fun hideMoreButton() {
        layoutMore.visibility = View.GONE
    }

    /**
     * 右边的按钮的点击事件
     */
    fun setMoreButtonOnClickListener(listener: ((View) -> Unit)? = null) {
        layoutMore.setOnClickListener(listener)
    }
}